package main

import (
	"crypto/ecdsa"
	"crypto/elliptic"
	"crypto/rand"
	"crypto/sha256"
	"encoding/hex"
	"fmt"
	"math/big"
)


func main() {
	//生成椭圆
	curve := elliptic.P256()
	privateKey, err := ecdsa.GenerateKey(curve, rand.Reader) //私钥
	if err != nil {
		panic(err)
	}
	publicKey := privateKey.PublicKey //公钥
	//对数据进行私钥签名
	data := "我是一条测试数据呀呀呀。"
	datahash := sha256.Sum256([]byte(data))

	//对hash值进行签名
	r, s, err := ecdsa.Sign(rand.Reader, privateKey, datahash[:])
	//把签名内容，r，s合到一起,r,s长度是一样的。
	signature := append(r.Bytes(), s.Bytes()...)
	if err != nil {
		panic(err)
	}

	//假设数据传输中---数据，公钥，签名signature
	//公钥验证
	datahash2 := sha256.Sum256([]byte(data))
	var r1 big.Int
	var s1 big.Int
	r1Data := signature[:len(signature)/2]
	s1Data := signature[len(signature)/2:]
	r1.SetBytes(r1Data)
	s1.SetBytes(s1Data)
	res := ecdsa.Verify(&publicKey, datahash2[:], &r1, &s1)
	fmt.Println("res=", res)

	pbyte:=privateKey.D.Bytes()
	fmt.Println("此时私钥钥为：",pbyte)
	fmt.Println("hex此时私钥钥为：",hex.EncodeToString(pbyte))
	//var p1 big.Int
	//p1.SetBytes(pbyte)


	//var siYaoString:="8c4758e7733acd292681bf446aa7356ece6e90e332bbd2b25252daa6479f6b2d"

}
